package com.yxh.lib_base.base

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintSet
import com.yxh.lib_base.databinding.ActivityBaseTitleBinding
import com.yxh.lib_base.ext.view.clickNoRepeat
import com.yxh.lib_base.ext.view.visibleOrGone
import com.yxh.lib_base.ext.viewbinding.binding

/**
 * @Author Roy
 * @DATE 2022/8/16 13:19
 * @Description
 */
abstract class BaseTitleActivity : BaseActivity() {

    private val _Binding by binding<ActivityBaseTitleBinding>()

    //是否是重叠标题栏
    private var isOverlapping = false

    /**
     * 当前Fragment绑定的视图布局
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        replaceContentWithView(_Binding.absContent, mBinding.root)
        _Binding.titleBar.activityBaseTitle.text =actionTitle()
        _Binding.titleBar.activityBaseBack.setOnClickListener {
            finish()
        }
        super.onCreate(savedInstanceState)
        if (isOverlapping) {
            setOverlappingTitleBar()
            initStatusBar(_Binding.titleBar.root)
        } else {
            initStatusBar(_Binding.absRootLayout)
        }
    }

    /**
     * 设置标题
     */
    protected fun setActionTitle(title: String) {
        _Binding.titleBar.activityBaseTitle.text = title
    }

    /**
     * 设置标题颜色
     */
    protected fun setActionTitleColor(titleColor: Int) {
        _Binding.titleBar.activityBaseTitle.setTextColor(resources.getColor(titleColor, null))
    }

    abstract fun actionTitle(): String

    /**
     * 启用重叠标题栏跟
     */
    protected fun startOverlappingTitleBar() {
        this.isOverlapping = true
    }

    /**
     * 更改返回图标
     */
    protected fun setBackIcon(resId: Int) {
        _Binding.titleBar.activityBaseBackIcon.setImageResource(resId)
    }

    protected fun addTitleRightBtn(title: String,onClick:()->Unit){
        _Binding.titleBar.activityBaseRTv.visibleOrGone(!title.isNullOrEmpty())?.let {
            it.text = title
            it.clickNoRepeat {
                onClick.invoke()
            }
        }
    }

    /**
     *  <替换 View>
     *  <使 DataBinding 替换 ViewStub 的方案>
     *      ~~ 参考viewStub
     */
    private fun replaceContentWithView(viewOld: View, replaceView: View) {
        val parent = viewOld.parent as ViewGroup
        val index = parent.indexOfChild(viewOld)
        parent.removeViewInLayout(viewOld)
        val layoutParams: ViewGroup.LayoutParams = viewOld.layoutParams
        if (layoutParams != null) {
            parent.addView(replaceView, index, layoutParams)
        } else {
            parent.addView(replaceView, index)
        }
    }

    /**
     * 设置重叠的标题栏
     */
    private fun setOverlappingTitleBar() {
        val constraintSet = ConstraintSet()
        constraintSet.clone(_Binding.absRootLayout)
        constraintSet.connect(_Binding.absRootLayout.id,
            ConstraintSet.TOP,
            ConstraintSet.PARENT_ID,
            ConstraintSet.TOP,
            0)
        constraintSet.applyTo(_Binding.absRootLayout)
    }

    /**
     * 初始化状态栏
     */
    open fun initStatusBar(view: View) {
        initStatusBar {
            statusBarDarkFont(true)
            titleBar(view)
        }
    }
}